diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-10-18 21:47:21 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-10-18 21:47:21 +0200 |
| commit | c3c61894d2b119ae424bac605d4b3099e0b674e5 (patch) | |
| tree | c525ebf4d33cbdfac22fc8702367f20d8f7ba344 /src/routes/grocery/[id]/+page.server.ts | |
| parent | c2722b62832afe2d028f1bcb6954116f7eec9cde (diff) | |
fix: report a duplicate name instead of failing the page
Adding a category or project whose name exists, or renaming an item onto an
existing name, hit the unique constraint and rendered SvelteKit's 500 page.
Translate SQLITE_CONSTRAINT_UNIQUE into a form error on the three forms.
Diffstat (limited to 'src/routes/grocery/[id]/+page.server.ts')
| -rw-r--r-- | src/routes/grocery/[id]/+page.server.ts | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/routes/grocery/[id]/+page.server.ts b/src/routes/grocery/[id]/+page.server.ts index a4ad377..232568c 100644 --- a/src/routes/grocery/[id]/+page.server.ts +++ b/src/routes/grocery/[id]/+page.server.ts @@ -16,6 +16,7 @@ import { parseFormString, parseOptionalNumber, } from '$lib/server/services/validation'; +import { isUniqueViolation } from '$lib/server/db/errors'; import type { Category, Grocery, Reference } from '$lib/server/services/types'; import type { Actions, PageServerLoad } from './$types'; @@ -86,7 +87,14 @@ export const actions: Actions = { return fail(400, { error: 'Le nom est requis' }); } - await updateGrocery(groceryId, name, categoryId); + try { + await updateGrocery(groceryId, name, categoryId); + } catch (error) { + if (isUniqueViolation(error)) { + return fail(400, { error: 'Un article porte déjà ce nom' }); + } + throw error; + } return { success: true, type: 'update' as const }; }, |
